package swing.chapter02;

import swing.util.SwingConsle;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class TextArea extends JFrame {
    private JButton addBtn = new JButton("新增"), clearBtn = new JButton("清除");
    private JTextArea textArea = new JTextArea(20, 40);
    private Random random = new Random();

    public TextArea(){
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.append(random.nextInt(1000) + ", ");
            }
        });

        clearBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("");
            }
        });

        setLayout(new FlowLayout());
        // JTextArea 使用 JScrollPane 包裹，当屏幕中文本太多的时候用它来进行滚动控制
        add(new JScrollPane(textArea));
        add(addBtn);
        add(clearBtn);
    }

    public static void main(String[] args) {
        SwingConsle.run(new TextArea(), 475,435);
    }
}
